﻿using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using System.Linq;
using WD.Db.Contract;
using WD.Db.Contract.Model;
using WD.Framework.Contract;
using WD.Web.MVC.Common;
using WD.Web.MVC.Controllers.Filters;
using WD.Framework.Utility;

namespace WD.Web.MVC.Areas.BaseArea.Controllers
{
    public class Rbac_UserController : AdminControllerBase
    {
        //
        // GET: /Manager/Rbac_User/
        [AdminOnlineFilter]
        public ActionResult Index(BaseRequest request)
        {
            var result = this.Rbac_UserService.GetRbac_UserList(request);
            return View(result);
        }
        [AdminOnlineFilter]
        public ActionResult Create(BaseRequest request)
        {
            var model = new Rbac_User();
            return View("Create", model);
        }

        [HttpPost]
        [AdminOnlineFilter]
        public string Create(FormCollection collection)
        {
            var ajax = new AjaxResult();
            var model = new Rbac_User();
            TryUpdateModel(model);
            try
            {
                Rbac_UserService.SaveRbac_User(model, LoginInfo.LoginId);
            }
            catch (BusinessException e)
            {
                // ModelState.AddModelError(e.Name, e.Message);
                return ajax.Err(e.Message);
            }

            return ajax.Suc("数据保存成功!");
        }

        [AdminOnlineFilter(Url = "/BaseArea/Rbac_User/Edit")]
        public ActionResult Edit(int id)
        {
            var model = Rbac_UserService.GetRbac_User(id);
            return View("Edit", model);
        }

        [HttpPost]
        [AdminOnlineFilter(Url = "/BaseArea/Rbac_User/Edit_Save")]
        public string Edit(int id, FormCollection collection)
        {
            var ajax = new AjaxResult();
            var model = Rbac_UserService.GetRbac_User(id);
            var pwd = model.PWD;
            TryUpdateModel(model);

            if (string.IsNullOrEmpty(model.PWD))
            {
                model.PWD = pwd;
            }
            else {
                if (model.PWD.Length < 6)
                {
                    return ajax.Err("密码长度需为6-20位!");
                }
                model.PWD = Encrypt.MD5(model.PWD);
            }
            Rbac_UserService.SaveRbac_User(model, LoginInfo.LoginId);
            return ajax.Suc("信息修改成功!");
        }

        [HttpPost]
        [AdminOnlineFilter]
        public string Deleted(List<int> ids)
        {
            var ajax = new AjaxResult();

            try
            {
                Rbac_UserService.DeleteRbac_User(ids, LoginInfo.LoginId);
            }
            catch (BusinessException e)
            {
                return ajax.Err(e.Message);
            }

            return ajax.Suc("信息已成功删除!");
        }

        [AdminOnlineFilter(Url = "/BaseArea/Rbac_User/SetRole")]
        public ActionResult SetRole(int id)
        {
            var re = new BaseRequest { PageSize = 9999 };
            //需要using System.Linq;
            //获取角色的全部数据
            var rolealllist = Rbac_RoleService.GetRbac_RoleList(re).ToList();
            //获取当前要分配角色的用户
            var model = Rbac_UserService.GetRbac_User(id);
            //当前用回对应的角色信息
            var rolelist = model.Rbac_Role.ToList();

            var tmpZtreeList = rolealllist.Select(x => new ZtreeNode()
                {
                    id = x.ID.ToString(CultureInfo.InvariantCulture),
                    //pId=
                    name = x.RoleName,
                    //  open=true
                    @checked = rolelist.Any(c => c.ID == x.ID)//是否选中
                }).ToList();

            //    var resultList = ToZtreeNode(tmpZtreeList, "");//转换上下级关系
            ViewBag.Nodes = JsonP(tmpZtreeList);
            ViewBag.id = id;
            return View();
        }

        [HttpPost]
        [AdminOnlineFilter(Url = "/BaseArea/Rbac_User/SetRole")]
        public string SetRole(int id, string ids)
        {

            var ajax = new AjaxResult();
            ids = ids.TrimEnd(',');
            var idList = new List<decimal>();
            if (!string.IsNullOrWhiteSpace(ids))
            {
                idList = ids.Split(',').Select(decimal.Parse).ToList();
            }
            Rbac_UserService.SetRbac_UserRoleList(id, idList);
            return ajax.Suc("用户分配角色成功!");

        }
    }
}
